<?php 

class Measurement_Model extends grocery_CRUD_Model 
{
    function __construct() 
    {
           parent::__construct();
    }
    
    function updateAccStock($id)
    {        
        $query ="UPDATE ACCESORIO
            SET ACC_STOCK=ACC_STOCK-1
            WHERE ACC_ID=".$id;
        $this->db->query($query)->result();        
	return TRUE;        
    }  
    
    function getAccIdCounter($id)
    {        
        $query ="SELECT ACC_ID
        FROM  CONTADOR  
        WHERE CON_SERIAL=".$id;
        $results = $this->db->query($query)->result();
        $tam = sizeof($results);
        if($tam>0){
            return $results[$tam-1]->ACC_ID;
        }        
	return NULL;        
    }     
    
    function updateCounter($data, $id){           
        $this->db->where('CON_SERIAL', $id);
        return $this->db->update('CONTADOR', $data);
    }
    
//    function insertMAC($data){           
//        return $this->db->insert('MATRICULAADQUIERECONTADOR',$data);
//    }
//    
//    function insertUMC($data){           
//        return $this->db->insert('USUARIOMATRICULACONEXION',$data);
//    }
    
    function insertMeasurement($data)
    {
        $this->db->insert('LECTURA',$data);
    }
    
    function validMeasurement($measurement,$serial)
    {
        $query ="SELECT CON_CAPACIDAD  FROM CONTADOR WHERE CON_SERIAL =".$serial;
        $result_counter= $this->db->query($query)->result();
        
        foreach ($result_counter as $row)
        {
            if($row->CON_CAPACIDAD>$measurement && $measurement>=0){
                return true;
            }
            return false;            
        }
        return false;
    }
    
       function get_list()
    {
    	if($this->table_name === null)
    		return false;
               
    	$select = "MATRICULA.MAT_ID, USUARIO.USUA_CEDULA, USUARIO.USUA_NOMBRES, USUARIO.USUA_APELLIDOS, MATRICULA.MAT_DIRECCIONINSTALACION, CONTADOR.CON_SERIAL , CONTADOR.CON_CAPACIDAD";
//        $select .= ",MATRICULA.MAT_ID";
        
//        $this->db->join('MATRICULA','MATRICULA.MAT_ID = LECTURA.MAT_ID');        
        $this->db->join('USUARIOMATRICULACONEXION','USUARIOMATRICULACONEXION.MAT_ID = MATRICULA.MAT_ID');
        $this->db->join('MATRICULAADQUIERECONTADOR','MATRICULAADQUIERECONTADOR.MAT_ID = MATRICULA.MAT_ID');
        $this->db->join('CONTADOR','CONTADOR.CON_SERIAL = MATRICULAADQUIERECONTADOR.CON_SERIAL');
        $this->db->join('USUARIO','USUARIO.USUA_ID = USUARIOMATRICULACONEXION.USUA_ID');
        
    	$this->db->select($select, false);
    	$results = $this->db->get($this->table_name)->result();	
        $cont=0;
        foreach ($results as $row) {
            $Lec_ant = $this->getListMeasurementsByIdEnrollment($row->MAT_ID);
            $results[$cont]->LEC_ANT = $Lec_ant;            
            $cont++;
        }
        $this->session->set_userdata('registros',  sizeof($results));
        return $results;
    } 
    
    function getListMeasurementsByIdEnrollment($id)
    {        
        $query ="SELECT *
        FROM  LECTURA  
        WHERE MAT_ID=".$id ." ORDER BY LEC_ID";
        $results = $this->db->query($query)->result();
        $tam = sizeof($results);
        if($tam>0){
            return $results[$tam-1]->LEC_VALORMEDICION;
        }        
	return 0;        
    }   
    
 
}

?>